package com.beaut.project.control;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public abstract class BaseControl {
	 /**
     * 将request参数转换成Map
     * @param request
     * @return
     */
    public  Map<String, Object> requestParameterToMap(HttpServletRequest request)
    {
        Map<String, Object> m = new HashMap<String, Object>();
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements())
        {
            String name = names.nextElement();
            m.put(name, request.getParameter(name).trim());
        }
        
        //统一为附件添加上传人id和上传人姓名属性-----start
        Map<String, Object> sessionMap = getSessionUser(request);
        if(sessionMap != null){
        	m.put("att_user_id", sessionMap.get("id"));
        	m.put("att_user_name", sessionMap.get("truename"));
        	
        	m.put("login_user_id", sessionMap.get("id"));//添加登录用户的id
        }
		//统一为附件添加上传人id和上传人姓名属性-----end
        return m;
    }
    /**
     * 将request参数转换成Map----用于需要加用户信息的时候
     * @param request
     * @return
     */
    public  Map<String, Object> requestParameterToMapUser(HttpServletRequest request)
    {
    	Map<String, Object> m = new HashMap<String, Object>();
    	Enumeration<String> names = request.getParameterNames();
    	while (names.hasMoreElements())
    	{
    		String name = names.nextElement();
    		m.put(name, request.getParameter(name).trim());
    	}
    	Map<String,Object> mapUser = getSessionUser(request);
    	if(mapUser != null){
	    	m.put("author_id",mapUser.get("id"));
	    	m.put("author_name",mapUser.get("truename"));
	    	
	    	//统一为附件添加上传人id和上传人姓名属性-----start
        	m.put("att_user_id", mapUser.get("id"));
        	m.put("att_user_name", mapUser.get("truename"));
        	//统一为附件添加上传人id和上传人姓名属性-----end
        }
    	return m;
    }
    
	/**
	 * 获取切面产生的pager对象值
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> getPagerMap(HttpServletRequest request) {
		return (Map<String, Object>) request.getAttribute("pageMap");
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> getSessionUser(HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		return (Map<String, Object>) session.getAttribute("LOGIN_USER");
	}
	
	public boolean isAdmin(HttpServletRequest request) {
		Map<String,Object>mapUser=getSessionUser(request);
		if(mapUser.get("username").equals("admin")){
			return true;
		}
    	return false;
	}
	@SuppressWarnings("unchecked")
	public String getSessionUserId(HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		Map<String, Object> m=(Map<String, Object>) session.getAttribute("LOGIN_USER");
		if(m != null){
			return m.get("id").toString();
		}
		return "";
	}
}
